<?php

class Product extends \Eloquent {
    protected $fillable = ['product_image_thumbnail'];
    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'Product';

    public static function getList($conditions=array(), $pageNumber = 5) {
        $result = self::query();

        if (isset($conditions['keySearch']) && $conditions['keySearch']) {
            $result->where('product_name', 'LIKE', '%'.$conditions['keySearch'].'%')
                   ->orWhere('product_model', 'LIKE', '%'.$conditions['keySearch'].'%');
        }

        if (isset($conditions['manufacturer_id']) && $conditions['manufacturer_id']) {
            $result->where('manufacturer_id', '=', $conditions['manufacturer_id']);
        }

        if (isset($conditions['category_id']) && $conditions['category_id']) {
            $result->where('category_id', '=', $conditions['category_id']);
        }

        $result->where('status', '<>', 0)
               ->where('delete_flag', '<>', 1)
               ->orderBy('updated_at', 'DESC');

        return $result->paginate($pageNumber);
    }

    public static function listProduct($conditions=array(), $pageNumber = 5) {
        $result = self::query();

        if (isset($conditions['keySearch']) && $conditions['keySearch']) {
            $result->where('product_name', 'LIKE', '%'.$conditions['keySearch'].'%')
                ->orWhere('product_model', 'LIKE', '%'.$conditions['keySearch'].'%');
        }

        if (isset($conditions['manufacturer_id']) && $conditions['manufacturer_id']) {
            $result->where('manufacturer_id', '=', $conditions['manufacturer_id']);
        }

        if (isset($conditions['category_id']) && $conditions['category_id']) {
            $result->where('category_id', '=', $conditions['category_id']);
        }

        $result->where('delete_flag', '<>', 1)->orderBy('updated_at', 'DESC');

        return $result->paginate($pageNumber);
    }

    public function manufacturer() {
        return $this->belongsTo('Manufacturer', 'manufacturer_id');
    }

    public function category() {
        return $this->belongsTo('Category', 'category_id');
    }

    public function createdId() {
        return $this->belongsTo('Users', 'created_id');
    }
}